//
//  HuoYuanDetailLatestVC.m
//  NtoccDriverNew
//
//  Created by Apple on 2020/12/25.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "HuoYuanDetailLatestVC.h"
#import "CYWebViewViewController.h"
#import "CJTextField.h"

#define DefaultLocationTimeout 10
#define DefaultReGeocodeTimeout 5

@interface HuoYuanDetailLatestVC ()<AMapLocationManagerDelegate>{
    bool _isAgree;
    BOOL _isRequesting;
    BOOL _isShowQRLoad;
    BOOL _isShowQRUnLoad;
    NSString *_timeString;
    NSInteger _orderState;//订单类型
}
@property (nonatomic,strong) UIScrollView*scrollview;

@property (nonatomic,strong) UIView *orderTakingView;//上方接单
@property (nonatomic,strong) UIView *transportNumView;//承运数量
@property (nonatomic,strong) UIView *transportCarView;//承运车数
@property (nonatomic,strong) UIView *priceUnitView;//报价单价
@property (nonatomic,strong) UIView *arriveTimeView;//到达时间
@property (nonatomic,strong) UIView *QRCodeContainerView;//装卸货码容器
@property (nonatomic,strong) UIView *qrLoadView;//
@property (nonatomic,strong) UIView *qrUnLoadView;//
@property (nonatomic,strong) UIView *hwsshyView;//货物所属行业

@property (nonatomic,strong) CJTextField *transportNumTF;
@property (nonatomic,strong) CJTextField *transportCarTF;
@property (nonatomic,strong) CJTextField *priceUnitTF;
@property (nonatomic,strong) CJTextField *arriveTimeTF;

@property (nonatomic,strong) UILabel *branchNameLab;
@property (nonatomic,strong) UILabel *transportNumLabel;//程运输量
@property (nonatomic,strong) UILabel *submitBtnLabel;
@property (nonatomic,strong) UILabel *orderCodeLabel;//货源单号
@property (nonatomic,strong) UILabel *loadAdressLabel;//装货地址
@property (nonatomic,strong) UILabel *unLoadAdressLabel;//卸货地址
@property (nonatomic,strong) UILabel *sendGoodsPersonLabel;//发货人
@property (nonatomic,strong) UILabel *sendGoodsPhoneLabel;//发货人
//@[@"货物名称",@"货物所属行业", @"需求车数", @"未接车数", @"货物数量", @"未接数量", @"合理损耗", @"装车时间", @"卸车时间", @"距离"];
@property (nonatomic,strong) UILabel *hwmcLabel;
@property (nonatomic,strong) UILabel *hwsshyLabel;
@property (nonatomic,strong) UILabel *xqcsLabel;
@property (nonatomic,strong) UILabel *wjcsLabel;
@property (nonatomic,strong) UILabel *hwslLabel;
@property (nonatomic,strong) UILabel *wjslLabel;
@property (nonatomic,strong) UILabel *hlshLabel;
@property (nonatomic,strong) UILabel *zcsjLabel;
@property (nonatomic,strong) UILabel *xcsjLabel;
@property (nonatomic,strong) UILabel *jlLabel;
@property (nonatomic,strong) UILabel *hwdjLabel;//货运单间
@property (nonatomic,strong) UILabel *yfdjLabel;//运费单间
@property (nonatomic,strong) UILabel *yclxLabel;//用车类型
@property (nonatomic,strong) UILabel *ysyqLabel;//运输要求
@property (nonatomic,strong) UILabel *insuranceLabel;
@property (nonatomic,strong) UIView *insuranceView;

@property (nonatomic,strong) UIImageView *agreeImageView;
@property (nonatomic,strong) UIImageView *qrLoadImageview;//装货码图
@property (nonatomic,strong) UIImageView *qrUnLoadImageview;//卸货码图
@property (nonatomic,strong) UIImageView *qrLoadTagImageview;
@property (nonatomic,strong) UIImageView *qrUnLoadTagImageview;

@property(nonatomic, strong)AMapLocationManager *locationManager;
@property(nonatomic, strong)NSDictionary        *dataDic;
@end

@implementation HuoYuanDetailLatestVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    _isAgree = NO;
    _orderState = 0;
    _isRequesting = NO;

    [self configLocationManager];
    [self loadData];
}

-(void)setupUI{
    
    self.title = @"货源详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    self.branchNameLab.text = CJStringWithFormat(@"《%@平台与承运方货物运输交易协议》", APP_PlatformName);
    self.branchNameLab.textColor = Theme_ColorValue;
}

- (void)loadData {
    
//    0独立司机 1车队 2车队司机
//    DebugLog(@"司机身份%@", [UserDefaultUtil valueForKey:DRIVER_TYPE]);
//    isQuote 0非报价
//    coalNumber 0货源单
    WEAKSELF;
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@/%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER,_orderCode) withParameter:@{@"token":[UserDefaultUtil valueForKey:USER_TOKEN],} withVC:self netName:@"货源详情信息" success:^(id response, NSString *msg) {
        _dataDic = response;
    
        [weakSelf reloadUI];
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

-(void)reloadUI{
    /**
     0 非报价单 货源单 独立司机
     1 非报价单 非货源单 独立司机
     2 报价单 货源单 独立司机
     3 报价单 非货源单 独立司机
     4 非报价单 货源单 车队
     5 非报价单 非货源单 车队
     6 报价单 货源单 车队
     7 报价单 非货源单 车队
     */
    
    if ([_dataDic[@"coalUnit"] integerValue] == 0) {
        _transportNumLabel.text = @"承运数量(吨)";
    } else if ([_dataDic[@"coalUnit"] integerValue] == 1) {
        _transportNumLabel.text = @"承运数量(方)";
    } else if ([_dataDic[@"coalUnit"] integerValue] == 2) {
        _transportNumLabel.text = @"承运数量(件)";
        _transportNumTF.textFieldType = CJTextFieldStringTypeNumber;
        _transportNumTF.keyboardType = UIKeyboardTypeNumberPad;
        _transportNumTF.maxLength = 10;
    }
    
    //报价单 0不允许报价
    NSString *isBaoJiaDan = CJStringWithFormat(@"%@",_dataDic[@"isQuote"]);
    //货源单 0货源单
    NSString *isHuoYuanDan = CJStringWithFormat(@"%@",_dataDic[@"coalNumber"]);
    //0独立司机 1车队 2车队司机
    NSString *driverType = CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]);
    
    //车队司机不能接单 需要车队给其分配
    if ([isBaoJiaDan isEqual:@"0"]) {
        if ([isHuoYuanDan isEqual:@"0"]) {
            if ([driverType isEqual:@"0"]) {
                //非报价单 货源单 独立司机
                _orderState = 0;
                self.orderTakingView.hidden = NO;
                self.transportNumView.hidden = NO;
                self.submitBtnLabel.text = @"接单";
                
            } else {
                if ([driverType isEqual:@"2"]) {
                    //车队司机不能接单 隐藏接单区域
                } else {
                    //非报价单 货源单 车队
                    _orderState = 4;
                    self.orderTakingView.hidden = NO;
                    self.transportNumView.hidden = NO;
                    self.transportCarView.hidden = NO;
                    self.submitBtnLabel.text = @"批量接单";
                }
            }
        } else {
            if ([driverType isEqual:@"0"]) {
                //非报价单 非货源单 独立司机
                _orderState = 1;
                self.submitBtnLabel.text = @"接单";
                self.orderTakingView.hidden = NO;
            } else {
                if ([driverType isEqual:@"2"]) {
                    //车队司机不能接单 隐藏接单区域
                } else {
                    //非报价单 非货源单 车队
                    _orderState = 5;
                    self.orderTakingView.hidden = NO;
                    self.transportCarView.hidden = NO;
                    self.submitBtnLabel.text = @"批量接单";
                }
            }
            
        }
    } else {
        if ([isHuoYuanDan isEqual:@"0"]) {
            if ([driverType isEqual:@"0"]) {
                //报价单 货源单 独立司机
                _orderState = 2;
                self.orderTakingView.hidden = NO;
                self.transportNumView.hidden = NO;
                self.priceUnitView.hidden = NO;
                self.arriveTimeView.hidden = NO;
                self.submitBtnLabel.text = @"报价承运";
            } else {
                if ([driverType isEqual:@"2"]) {
                    //车队司机不能接单 隐藏接单区域
                } else {
                    //报价单 货源单 车队
                    _orderState = 6;
                    
                    self.transportNumView.hidden = NO;
                    self.transportCarView.hidden = NO;
                    self.priceUnitView.hidden = NO;
                    self.arriveTimeView.hidden = NO;
                    self.submitBtnLabel.text = @"批量报价";
                }
            }
        } else {
            if ([driverType isEqual:@"0"]) {
                //报价单 非货源单 独立司机
                _orderState = 3;
                
                self.priceUnitView.hidden = NO;
                self.arriveTimeView.hidden = NO;
                self.submitBtnLabel.text = @"报价承运";
            } else {
                if ([driverType isEqual:@"2"]) {
                    //车队司机不能接单 隐藏接单区域
                } else {
                    //报价单 非货源单 车队
                    _orderState = 7;
                    
                    self.transportCarView.hidden = NO;
                    self.priceUnitView.hidden = NO;
                    self.arriveTimeView.hidden = NO;
                    self.submitBtnLabel.text = @"批量报价";
                }
            }
        }
    }
    
    //货源单号
    self.orderCodeLabel.text = CJStringWithFormat(@"%@",_dataDic[@"mainOrderNumber"]);
    
    //装卸货码
    if ([_dataDic[@"qrCodeOnOff"] integerValue] == 1 && ([driverType isEqual:@"0"] || [driverType isEqual:@"2"])) {
        self.QRCodeContainerView.hidden = NO;
        self.qrLoadTagImageview.contentMode = UIViewContentModeScaleAspectFit;
        self.qrUnLoadImageview.contentMode = UIViewContentModeScaleAspectFit;

        NSString *s1 =  _dataDic[@"loadingQr"] == NULL ? @"" : _dataDic[@"loadingQr"];
        NSString *s2 =  _dataDic[@"unloadingQr"] == NULL ? @"" : _dataDic[@"unloadingQr"];
        [self.qrLoadImageview sd_setImageWithURL:[NSURL URLWithString:s1] placeholderImage:image(@"gcy_icon_plaholder")];
        [self.qrUnLoadImageview sd_setImageWithURL:[NSURL URLWithString:s2] placeholderImage:image(@"gcy_icon_plaholder")];
    }
    
    //地址
    self.loadAdressLabel.text =  CJStringWithFormat(@"%@%@", _dataDic[@"shipperAddress"], _dataDic[@"shipperDetailAddress"]);
    self.unLoadAdressLabel.text =  CJStringWithFormat(@"%@%@", _dataDic[@"consigneeAddress"], _dataDic[@"consigneeDetailAddress"]);
    self.sendGoodsPersonLabel.text = CJStringWithFormat(@"%@",_dataDic[@"shipperName"]);
    self.sendGoodsPhoneLabel.text = CJStringWithFormat(@"%@",_dataDic[@"shipperPhone"]);
    
    //货物信息
    if ([[UserDefaultUtil valueForKey:Login_Branch][@"isTax"] integerValue] == 1) {
        self.hwsshyView.hidden = NO;
    }
    //货物信息
    NSString *coalNumber = @"";
    NSString *notReceivedCount = @"";
    NSString *limitWastageAmount = @"";
    
    if ([_dataDic[@"coalNumber"] integerValue] == 0) {
        coalNumber = @"不限";
    } else{
        coalNumber = CJStringWithFormat(@"%@车",_dataDic[@"coalNumber"]);
    }
    
    if ([_dataDic[@"notReceivedCount"] integerValue] == 0) {
        notReceivedCount = @"不限";
    } else {
        notReceivedCount = CJStringWithFormat(@"%@车",_dataDic[@"notReceivedCount"]);
    }
    
    if ([_dataDic[@"limitWastageAmount"] integerValue] == 0) {
        limitWastageAmount = @"不计";
    } else {
        limitWastageAmount = CJStringWithFormat(@"%@%@",_dataDic[@"limitWastageAmount"], _dataDic[@"limitWastageTypeName"]);//合理损耗
    }
    
    
    _hwmcLabel.text = CJStringWithFormat(@"%@",_dataDic[@"coalTypeName"]);
    _hwsshyLabel.text = [NSString nullToString:CJStringWithFormat(@"%@",_dataDic[@"industryName"])];
    _xqcsLabel.text = coalNumber;
    _wjcsLabel.text = notReceivedCount;
    _hwslLabel.text = CJStringWithFormat(@"%.3f%@",[_dataDic[@"weight"] floatValue],_dataDic[@"coalUnitName"]);
    _wjslLabel.text = CJStringWithFormat(@"%.3f%@",[_dataDic[@"lastWeight"] floatValue],_dataDic[@"coalUnitName"]);
    _hlshLabel.text = limitWastageAmount;
    _zcsjLabel.text = CJStringWithFormat(@"%@",_dataDic[@"lastLoadingTime"]);
    _xcsjLabel.text = CJStringWithFormat(@"%@",_dataDic[@"lastUnloadingTime"]);
    _jlLabel.text = [NSString stringWithFormat:@"%@公里", [NSString nullToString:CJStringWithFormat(@"%.2f", [_dataDic[@"distance"] floatValue])]];
    
    //费用信息
    if ([_dataDic[@"isQuote"] integerValue] == 1) {
        _hwdjLabel.text = CJStringWithFormat(@"%.2f元/%@", [_dataDic[@"coalPrice"] floatValue], _dataDic[@"coalUnitName"]);//货物单价
        _yfdjLabel.hidden = YES;
    } else {
        _yfdjLabel.hidden = NO;
        _hwdjLabel.text = CJStringWithFormat(@"%.2f元/%@", [_dataDic[@"coalPrice"] floatValue], _dataDic[@"coalUnitName"]);//货物单价
        _yfdjLabel.text = CJStringWithFormat(@"%.2f%@", [_dataDic[@"realPrice"] floatValue], _dataDic[@"isTrunkName"]);//运费单价
    }
    
    if ([_dataDic[@"orderDetail"][@"isInsure"] integerValue] == 1) {
        _insuranceView.hidden = NO;
        _insuranceLabel.text = [NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"insuranceFee"] floatValue])];
    }
    
    //其他信息
    _yclxLabel.text = [DCSpeedy isBlankString:CJStringWithFormat(@"%@",_dataDic[@"pushVehicleUse"])] == YES ? @"暂无" : CJStringWithFormat(@"%@",_dataDic[@"pushVehicleUse"]);
    _ysyqLabel.text = [NSString nullToString:CJStringWithFormat(@"%@",_dataDic[@"coalName"])];

}

//选择到达时间
- (void)btnArriveTimeAction{
    
    [self.view endEditing:YES];
    
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMDHM];
    datePickerView.title = @"选择日期";
    datePickerView.isAutoSelect = NO;
    datePickerView.showToday = NO; // 是否显示“今天”
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
//        NSLog(@"选择的值：%@", selectValue);
        _timeString = selectValue;
        self.arriveTimeTF.text = selectValue;
    };
    [datePickerView show:nil];
}

//报价承运
- (void)submitTransAction{
    
    [self.view endEditing:YES];
    if (_isRequesting == NO) {
        if (_isAgree == NO) {
            [XSInfoView showInfo:@"请先同意交易协议" onView:self.view];
            return;
        }
        
        NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
        NSString *urlString = @"";
        
        if (_orderState == 0) {
            if ([DCSpeedy isBlankString:_transportNumTF.text] == YES) {
                [XSInfoView showInfo:@"承运数量为空" onView:self.view];
                return;
            }
            
            [muDic setValue:_transportNumTF.text forKey:@"perWeight"];
            [muDic setValue:self.orderCode forKey:@"mainOrderCode"];
            
            urlString = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_DETAIL_RECEIVE);
            
        } else if (_orderState == 1) {
            
            [muDic setValue:self.orderCode forKey:@"mainOrderCode"];
            
            urlString = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_DETAIL_RECEIVE);
            
        } else if (_orderState == 2) {
            
            if ([DCSpeedy isBlankString:_transportNumTF.text] == YES || [DCSpeedy isBlankString:_priceUnitTF.text] == YES || [DCSpeedy isBlankString:_timeString] == YES) {
                [XSInfoView showInfo:@"信息不能为空" onView:self.view];
                return;
            }
            
            [muDic setValue:_transportNumTF.text forKey:@"bidWeight"];
            [muDic setValue:_priceUnitTF.text forKey:@"bidPrice"];
            [muDic setValue:_timeString forKey:@"arrivalTime"];
            [muDic setValue:self.orderCode forKey:@"mainOrderCode"];
            
            urlString = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_BID_BID);
            
        } else if (_orderState == 3) {
            
            if ([DCSpeedy isBlankString:_priceUnitTF.text] == YES || [DCSpeedy isBlankString:_timeString] == YES) {
                [XSInfoView showInfo:@"信息不能为空" onView:self.view];
                return;
            }
            
            [muDic setValue:_priceUnitTF.text forKey:@"bidPrice"];
            [muDic setValue:_timeString forKey:@"arrivalTime"];
            [muDic setValue:self.orderCode forKey:@"mainOrderCode"];
            
            urlString = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_BID_BID);
            
        } else if (_orderState == 4) {
            
            if ([DCSpeedy isBlankString:_transportNumTF.text] == YES || [DCSpeedy isBlankString:_transportCarTF.text] == YES) {
                [XSInfoView showInfo:@"信息不能为空" onView:self.view];
                return;
            }
            
            [muDic setValue:_transportNumTF.text forKey:@"perWeight"];
            [muDic setValue:_transportCarTF.text forKey:@"coalNumber"];
            [muDic setValue:self.orderCode forKey:@"mainOrderCode"];
            
            urlString = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_DETAIL_NEWMULTIRECEIVE);
            
        } else if (_orderState == 5) {
            
            if ([DCSpeedy isBlankString:_transportCarTF.text] == YES) {
                [XSInfoView showInfo:@"车数不能为空" onView:self.view];
                return;
            }
            
            [muDic setValue:_transportCarTF.text forKey:@"coalNumber"];
            [muDic setValue:self.orderCode forKey:@"mainOrderCode"];
            
            urlString = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_DETAIL_NEWMULTIRECEIVE);
            
        } else if (_orderState == 6) {
            
            if ([DCSpeedy isBlankString:_transportNumTF.text] == YES || [DCSpeedy isBlankString:_priceUnitTF.text] == YES || [DCSpeedy isBlankString:_transportCarTF.text] == YES ||[DCSpeedy isBlankString:_timeString] == YES) {
                [XSInfoView showInfo:@"信息为空" onView:self.view];
                return;
            }
            
            //            [muDic setValue:_shuLiangString forKey:@"perWeight"];
            [muDic setValue:_transportNumTF.text forKey:@"bidWeight"];
            [muDic setValue:_priceUnitTF.text forKey:@"bidPrice"];
            [muDic setValue:_transportCarTF.text forKey:@"bidCount"];
            [muDic setValue:_timeString forKey:@"arrivalTime"];
            [muDic setValue:self.orderCode forKey:@"mainOrderCode"];
            
            urlString = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_BID_NEWMULTIBID);
            
        } else if (_orderState == 7) {
            
            if ([DCSpeedy isBlankString:_priceUnitTF.text] == YES || [DCSpeedy isBlankString:_transportCarTF.text] == YES ||[DCSpeedy isBlankString:_timeString] == YES) {
                [XSInfoView showInfo:@"信息为空" onView:self.view];
                return;
            }
            
            [muDic setValue:_priceUnitTF.text forKey:@"bidPrice"];
            [muDic setValue:_transportCarTF.text forKey:@"bidCount"];
            [muDic setValue:_timeString forKey:@"arrivalTime"];
            [muDic setValue:self.orderCode forKey:@"mainOrderCode"];
            
            urlString = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl],ORDER_BID_NEWMULTIBID);
            
        }
        
        //6-20 新增经纬度参数
        [muDic setValue:[UserDefaultUtil valueForKey:LONGITUDE] forKey:@"longitude"];
        [muDic setValue:[UserDefaultUtil valueForKey:LATITUDE] forKey:@"latitude"];
        [muDic setValue:[UserDefaultUtil valueForKey:USER_TOKEN] forKey:@"token"];
       
        _isRequesting = YES;

        [HTTPManager postNetworkRequest:urlString withParameter:muDic withVC:self netName:@"接单" success:^(id response, NSString *msg) {
            
            [XSInfoView showInfo:msg onView:self.view];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                
                _isRequesting = NO;
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        } failure:^(NSInteger code, NSString *msg) {
            
            _isRequesting = NO;
            
        }];
        
    }
}

//同意协议
-(void)agreeEvent{
    _isAgree = !_isAgree;
    if (_isAgree) {
        [self.agreeImageView setImage:[image(@"gcy_img_home_business_select") imageChangeColor:Theme_ColorValue]];
    }else{
        [self.agreeImageView setImage:image(@"gcy_img_home_business_nomal")];
    }
}

//装货码
-(void)loadGoodsAction{
    _isShowQRLoad = !_isShowQRLoad;
    UIImage *backImage = [UIImage imageNamed:@"gcy_btn_my_rArrow"];
    if (_isShowQRLoad) {
        backImage = [UIImage imageWithCGImage:backImage.CGImage
                                        scale:backImage.scale
                                  orientation:UIImageOrientationRight];
    }
    [self.qrLoadTagImageview setImage:backImage];
    self.qrLoadView.hidden = !_isShowQRLoad;
}

//卸货码
-(void)unloadGoodsAction{
    _isShowQRUnLoad = !_isShowQRUnLoad;
    
    UIImage *backImage = [UIImage imageNamed:@"gcy_btn_my_rArrow"];
    if (_isShowQRUnLoad) {
        backImage = [UIImage imageWithCGImage:backImage.CGImage
                                        scale:backImage.scale
                                  orientation:UIImageOrientationRight];
    }
    [self.qrUnLoadTagImageview setImage:backImage];
    self.qrUnLoadView.hidden = !_isShowQRUnLoad;
}

//查看路线
-(void)lookRoute{
    NSString *shipperLongitude = [NSString nullToString:_dataDic[@"shipperLongitude"]];//装货地经度
    NSString *shipperLatitude = [NSString nullToString:_dataDic[@"shipperLatitude"]];//装货地纬度
    NSString *shipperAddress = [NSString nullToString:_dataDic[@"shipperAddress"]];//装货地地址
    
    NSString *consigneeLongitude = [NSString nullToString:_dataDic[@"consigneeLongitude"]];
    NSString *consigneeLatitude = [NSString nullToString:_dataDic[@"consigneeLatitude"]];
    NSString *consigneeAddress = [NSString nullToString:_dataDic[@"consigneeAddress"]];
    
    NSDictionary *dic = @{
        @"shipperLongitude": shipperLongitude,
        @"shipperLatitude": shipperLatitude,
        @"shipperAddress": shipperAddress,
        @"consigneeLongitude": consigneeLongitude,
        @"consigneeLatitude": consigneeLatitude,
        @"consigneeAddress": consigneeAddress,
    };
    CYWebViewViewController *webVC = [[CYWebViewViewController alloc] init];
    webVC.titleString = @"查看路线";
    webVC.urlString = CJStringWithFormat(@"%@/admin/#/enclosure?orderCode=%@", BasicDomainURL, _dataDic[@"code"]);
    webVC.state = 1;
    webVC.navDic = dic;
    [self.navigationController pushViewController:webVC animated:YES];
}

-(void)lookLoadGoods{
    [self showImage:@[_dataDic[@"loadingQr"] == NULL ? @"" : _dataDic[@"loadingQr"]] title:@"查看装货二维码"];
}
-(void)lookUnLoadGoods{
    [self showImage:@[_dataDic[@"unloadingQr"] == NULL ? @"" : _dataDic[@"unloadingQr"]] title:@"查看卸货二维码"];
}

-(void)showImage:(NSArray *)urlArr title:(NSString *)title{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *changeAction = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

        HZPhotoBrowser *browser = [[HZPhotoBrowser alloc] init];
        browser.isFullWidthForLandScape = YES;
        browser.isNeedLandscape = YES;
        browser.currentImageIndex =0;
        browser.imageArray = urlArr;
        [browser show];

    }];
    UIAlertAction *checkAction = [UIAlertAction actionWithTitle:@"保存二维码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

        if ([DCSpeedy isBlankString:[urlArr firstObject]]) {
            [XSInfoView showInfo:@"图片为空，无法保存" onView:self.view];
            return;
        }
        [self.qrLoadImageview sd_setImageWithURL:[NSURL URLWithString:[urlArr firstObject]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            // 保存图片
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }];

    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){

    }];

    [alertController addAction:changeAction];
    [alertController addAction:checkAction];
    [alertController addAction:cancelAction];

    [[DCSpeedy getcurrentTopViewController] presentViewController:alertController animated:YES completion:nil];
}

//查看协议
-(void)lookAgreeEvent{
    CYWebViewViewController *webVC = [[CYWebViewViewController alloc] init];
    webVC.titleString = @"运输交易协议";
    webVC.urlString = CJStringWithFormat(@"%@/agreement/agreement.html?agreementType=5", BasicDomainURL);
    webVC.state = 1;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)configLocationManager
{
    self.locationManager = [[AMapLocationManager alloc] init];
    
    [self.locationManager setDelegate:self];
    
    //设置期望定位精度
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    
    //设置定位超时时间
    [self.locationManager setLocationTimeout:DefaultLocationTimeout];
    
    //设置逆地理超时时间
    [self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
    
    //设置允许连续定位逆地理
    [self.locationManager setLocatingWithReGeocode:YES];
    
    [self.locationManager startUpdatingLocation];
}

- (void)cleanUpAction
{
    //停止定位
    [self.locationManager stopUpdatingLocation];
    
    [self.locationManager setDelegate:nil];
    
}

#pragma mark - AMapLocationManager Delegate

/**
 *  @brief 连续定位回调函数.注意：如果实现了本方法，则定位信息不会通过amapLocationManager:didUpdateLocation:方法回调。
 *  @param manager 定位 AMapLocationManager 类。
 *  @param location 定位结果。
 *  @param reGeocode 逆地理信息。
 */
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    DebugLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
    
    [UserDefaultUtil saveValue:CJStringWithFormat(@"%f",location.coordinate.longitude) forKey:LONGITUDE];
    [UserDefaultUtil saveValue:CJStringWithFormat(@"%f",location.coordinate.latitude) forKey:LATITUDE];
    
    //详细信息
    if (reGeocode)
    {
        DebugLog(@"reGeocode:%@", reGeocode);
    }
    
}

- (void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager *)locationManager
{
    [locationManager requestAlwaysAuthorization];
}

/**
 *  @brief 当定位发生错误时，会调用代理的此方法。
 *  @param manager 定位 AMapLocationManager 类。
 *  @param error 返回的错误，参考 CLError 。
 */
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error {
    
    DebugLog(@"定位error：%@",error);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        [XSInfoView showInfo:@"图片保存失败" onView:self.view];
        
    } else {
        [XSInfoView showInfo:@"图片保存成功" onView:self.view];
    }
}
    
- (void)dealloc
{
    [self cleanUpAction];
}

@end
